package com.huifaapp.mall.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.MyLvShareFriendListviewAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.ShareFriends;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.List;

/**
 * Created by Administrator on 2017/12/14.
 */

public class AFriendCircleFragment extends Fragment {
    private PullToRefreshListView Mylv_ShareFriend;
    MyLvShareFriendListviewAdapter adapter;
    List<ShareFriends.DataBean.ListBean> listb;
    int atat=0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View viewLayout = inflater.inflate(R.layout.home_fragment_a_friend_circle, container, false);

        Mylv_ShareFriend= (PullToRefreshListView)viewLayout.findViewById(R.id.Mylv_ShareFriend);
        LoadShareFriend(0,atat);
        Mylv_ShareFriend.setMode(PullToRefreshBase.Mode.BOTH);//两端刷新
        Mylv_ShareFriend.setSelected(true);
        Mylv_ShareFriend.setScrollingWhileRefreshingEnabled(true);
        Mylv_ShareFriend.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                LoadShareFriend(0,atat);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                LoadShareFriend(1,atat++);
                Log.e("数据一键分享朋友圈",listb+"");

            }
        });
        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                Mylv_ShareFriend.setRefreshing(true);
                return false;
            }
        }).sendEmptyMessageDelayed(0, 200);
        return viewLayout;
    }

    int length =5;
    int start=0;
    private void LoadShareFriend(final int downorup, int starts){
        if (downorup == 0) {
            length=5;
            start=0;
        } else if (downorup == 1) {
            length=5;
            start=starts;
        }
        String url =ApiManager.EVERYDAY_LIST+ "?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity())+ "&start=" + start + "&length=" + length+"&type=friend";

        Log.e("WEB", "路径ShareFriend--" + url);
        HttpUtils http = new HttpUtils();
        //http://www.jhjvip.cn:8081/market/mobile/jd/index?
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                  testTextView.setText(current + "/" + total);
                Log.e("数据", "数据-------" + current + "/" + total);
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
//                    textView.setText(responseInfo.result);
                Mylv_ShareFriend.onRefreshComplete();

                try {
                    Log.e("数据ShareFriend", "数据d-------" + responseInfo.result);
                    Gson gson = new Gson();
                    ShareFriends list = gson.fromJson(responseInfo.result, ShareFriends.class);
                    if(list.getCode().equals("10000")) {
                        if (list.getData().getList() != null && list.getData().getList().size() > 0) {
                            if (downorup == 0) {

                                listb = list.getData().getList();
                                Log.e("数据一键分享朋友圈",listb+"");
                                adapter = new MyLvShareFriendListviewAdapter(getActivity(), listb);
                                Mylv_ShareFriend.setAdapter(adapter);

                            } else {
                                listb.addAll(list.getData().getList());
                                if (listb.size() == 0) {
                                    Mylv_ShareFriend.setVisibility(View.GONE);
//                                tv_no_message.setVisibility(View.VISIBLE);
                                }
                                adapter.notifyDataSetChanged();
                                // listView.getRefreshableView().setSelection(a);
                            }
                        }
                    }else{
                        Toast.makeText(getActivity(),list.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }catch (JsonSyntaxException e){
                    Toast.makeText(getActivity(),"未知错误",Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Mylv_ShareFriend.onRefreshComplete();
            }
        });
    }
}
